define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/ec/kehu/index' + location.search,
                    add_url: 'zhongzhuan/ec/kehu/add',
                    edit_url: 'zhongzhuan/ec/kehu/edit',
                    del_url: 'zhongzhuan/ec/kehu/del',
                    multi_url: 'zhongzhuan/ec/kehu/multi',
                    import_url: 'zhongzhuan/ec/kehu/import',
                    table: 'ec_kehu',
                }
            });

            var table = $("#table");
            
            table.on('post-common-search.bs.table', function (event, table) {

                // 以下2行代码是关键，是加载完表格后，把销售搜索空实例化成selectpage格式
                var form = $("form",table.$commonsearch);
                
               
                $("input[name='follow_userid']",form).addClass("selectpage").data("source","zhongzhuan/ec/ecuser/index").data("primaryKey","userid");
                
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
                        
                        {field: 'userinfo.name', title: __('Follow_userid'),searchable:false, operate: 'LIKE',formatter: function(value, row, index) {
                            //value：intro字段的值
                            //row：当前行所有字段的数据
                            //index：当前行索引
                            //示例：
                            if(row.follow_userid== '' || row.follow_userid == 0){
                                return '公海';
                            }
                            return value + "[" + row.follow_userid +"]";
                        }},                       
                        {field: 'crmId', title: __('Crmid'), operate: 'LIKE'},
                        {field: 'follow_userid', title: '跟进人',visible:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, searchable:false,class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'yixiang_txt', title: __('Yixiang_txt'), operate: 'LIKE', searchList: Config.yixiang_list,table: table, class: 'autocontent', formatter: Table.api.formatter.content},                        
                        {field: 'record_file', title: __('Record_file'),formatter:Table.api.formatter.url,searchable:false},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            $(document).on("click",'.btn-auto-update', function () {
                console.log(111);
                layer.confirm('确认要自动更新已推送客户到本系统？', {
                    btn: ['确认'] //可以无限个按钮
                  }, function(index, layero){
                      layer.load(1); //风格1的加载
                     $.ajax({
                         url:'/api/ec.eckehu/batchGetKehuList',
                         type:'POST',
                         success:function(res){
                            layer.closeAll();
                            layer.msg(res.msg);
                            location.reload()
                         }
                     })
                  });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
